<?php

namespace App\Http\Requests\Api;

class MenuRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        switch ($this->route()->getName()) {
            case 'menus.store':
                return [
                    'path' => 'required|max:255',
                    'name' => 'required|max:30|unique:menus',
                    'label' => 'required|max:30',
                    //'pid' => 'required|exists:menus,id',
                    'icon' => 'sometimes|max:30',
                    'url' => 'required|max:255',
                ];

            case 'menus.update':
                $id = $this->segment(4); // 排除掉当前菜单
                return [
                    'path' => 'sometimes|max:255',
                    'name' => 'sometimes|max:30|unique:menus,name,' . $id,
                    'label' => 'sometimes|max:30',
                    //'pid' => 'sometimes|exists:menus,id',
                    'icon' => 'sometimes|max:30',
                    'url' => 'sometimes|max:255',
                ];

            default:
                ;
        }
    }

    public function messages(): array
    {
        return [
            'path.required' => '路径不能为空',
            'path.max' => '路径最大长度为255个字符',
            'name.required' => '路由名称不能为空',
            'name.max' => '路由名称最大长度为30个字符',
            'name.unique' => '路由名称已经存在',
            'label.required' => '菜单名称不能为空',
            'label.max' => '菜单名称最大长度为30个字符',
            'icon.max' => '图标最大长度为30个字符',
            'url.required' => '视图文件路径不能为空',
            'url.max' => '视图文件路径最大长度为255个字符',
        ];
    }
}
